<?php

namespace app\ljhs\controller;
use app\common\controller\Ljhs;
use app\admin\model\AdminLog;
use fast\Random;
use think\Config;
use think\Hook;
use think\Validate;
use app\common\model\Carcheck;
use app\common\model\Comrepair;
use app\common\model\Carjy;
use app\common\model\Carrepair;
/**
 * 用户接口
 */
class Car extends Ljhs
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 车况检测
     * @return void
     */
    public function carcheck()
    {
        $user = $this->auth->getUserInfo();
        if($user['groups']['group_id'] != 7){
            $this->error(__('您没有权限'));
        }
        $params = $this->request->post();
        if(!isset($params['oil_num']) || !isset($params['km'])){
            $this->error(__('参数错误'));
        }
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = Carcheck::create($params);
        if($res){

            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }


    /**
     * 车辆加油
     * @return void
     */
    public function carjy()
    {
        $user = $this->auth->getUserInfo();
        $params = $this->request->post();
        if(!isset($params['rise_num']) || !isset($params['km'])){
            $this->error(__('参数错误'));
        }
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = Carjy::create($params);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }



    /**
     * 车辆维修
     * @return void
     */
    public function carRepair()
    {
        $user = $this->auth->getUserInfo();
        $params = $this->request->post();
        if(!isset($params['comrepair_id']) || !isset($params['km']) || !isset($params['price'])){
            $this->error(__('参数错误'));
        }
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = Carrepair::create($params);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }



    /**
     * 获取维修企业列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRepair()
    {
        $user = $this->auth->getUserInfo();
        $list = Comrepair::where('stand_id',$user['stand_id'])->order('id desc')->select();
        $this->success(__('获取成功'),$list);
    }

    /**
     * 添加维修企业
     * @return void
     */
    public function addRepair()
    {
        $user = $this->auth->getUserInfo();
        $name = $this->request->post('name');
        if(!$name){
            $this->error(__('请输入维修企业名称'));
        }
        $insert = [
            'name' => $name,
            'stand_id' => $user['stand_id'],
            'createtime' => time()
        ];
        $res = Comrepair::create($insert);
        if($res){
            $this->success(__('添加成功'),$res);
        }else{
            $this->error(__('添加失败'));
        }
    }


}
